import router from './router'
// import store from './store'
// import {Toast} from 'vant'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import {getToken, getVenuesId} from '@/utils/auth' // 从Cookie获取token
// import tools from './utils/tools.js'

// NProgress配置
NProgress.configure({showSpinner: false})
NProgress.configure({easing: 'ease', speed: 500})

/// 不重定向白名单
const whiteList = ['/index', '/wechatLogin', '/bindPhone', '/userInforEntry', '/arrivalConfirm', '/loginForArrive']

router.beforeEach((to, from, next) => {
  NProgress.start() // 开始Progress
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (getToken()) {
    // 有token
    if (to.path === '/wechatLogin' && getVenuesId()) {
      // 用于扫码确认到达时的特殊处理
      next()
    } else if (to.path === '/wechatLogin') {
      // next({path: '/booking'})
      // tools.goHref('booking')
      next()
    } else {
      next()
    }
    NProgress.done()
  } else {
    // 没有token
    if (to.name === 'loginForArrive') {
      // 用于扫码确认到达的特殊处理
      next()
    } else if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      // 否则全部重定向到首页
      next({path: '/index'})
    }
    NProgress.done()
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})
